package com.common.baselibrary.mvp;


import com.common.baselibrary.util.NetworkHelper;
import com.common.baselibrary.util.ToastUtil;

import java.lang.ref.WeakReference;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by   on 2018/4/11.
 */

public class BasePresenter<V extends BaseView> {

    private WeakReference<V> mWeakReference;
    private CompositeDisposable mCompositeDisposable;
    private V mView;

    public void attach(V v) {
        try {
            if (mWeakReference == null) {
                mWeakReference = new WeakReference<V>(v);
            }
            mView = v;
            if (mCompositeDisposable == null) {
                mCompositeDisposable = new CompositeDisposable();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public <T> void addTask(Observable<T> observable, Consumer<T> consumer) {
        if (!NetworkHelper.isNetworkConnected()) {
            getView().hideLoading();
            getView().onNoNetwork();
        }
        mCompositeDisposable.add(
                observable
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(consumer, new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) {
                                getView().hideLoading();
                                throwable.printStackTrace();

                                //错误提示信息
                                if (throwable instanceof SocketTimeoutException) {
                                    ToastUtil.showShortToast("网络超时");
                                    getView().onTimeout();
                                } else if (throwable instanceof ConnectException) {
                                    getView().onNoNetwork();
                                    ToastUtil.showShortToast("网络连接失败");
                                } else if (throwable instanceof UnknownHostException) {
                                    getView().onNoNetwork();
                                    ToastUtil.showShortToast("无法连接到主机");
                                } else {
                                    getView().onNoNetwork();
                                    ToastUtil.showShortToast(throwable.toString());
                                }
                            }
                        }));
    }


//    public <T> void addTask(Observable<String> observable, final MyNetworkCallBack<T> myNetworkCallBack) {
//        mCompositeDisposable.add(
//                observable
//                        .subscribeOn(Schedulers.io())
//                        .observeOn(AndroidSchedulers.mainThread())
//                        .subscribe(new Consumer<String>() {
//                            @Override
//                            public void accept(String s) throws Exception {
//                                LogUtil.i(s);
//                                HttpRespond<T> httpRespond = new Gson().fromJson(s, new TypeToken<HttpRespond<T>>() {
//                                }.getType());
//                                int result = httpRespond.result;
//                                String message = httpRespond.message;
//
//                                //token失效或未登录
//                                if (result == -1) {
//                                    myNetworkCallBack.onTokenInvalid(result, message);
//                                    ic_back;
//                                }
//                                //请求成功
//                                if (result != 0) {
//                                    myNetworkCallBack.onSuccess(result, message, httpRespond.data);
//                                } else {
//                                    myNetworkCallBack.onFailed(result, message);
//                                }
//                            }
//                        }, new Consumer<Throwable>() {
//                            @Override
//                            public void accept(Throwable throwable) throws Exception {
//                                Log.e("http", "throwable: " + throwable.getMessage());
//                                V v = mWeakReference.get();
//                                if (v != null) {
//                                    v.onNetworkError();
//                                }
//                            }
//                        }));
//    }

    public void detach() {
        try {
            if ((!getView().noDisposeAfterDetach()) && !mCompositeDisposable.isDisposed()) {
                mCompositeDisposable.dispose();
                mCompositeDisposable = null;
            }
            mWeakReference = null;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public V getView() {
        mView = mWeakReference.get();
        return mView;
    }


}
